//
//  YCLPlaceHolderTextView.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/20.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLPlaceHolderTextView: UITextView {
    
    //占位文本（？？）
//    var placeHolder: String? {
//    
//        didSet{
//        
//            placeHolderLabel.text = placeHolder
//        }
//    }
//    
//    override var text: String! {
//    
//        didSet{
//        
//            //当被人给textview的text赋值时，隐藏站位文本
//            placeHolderLabel.hidden = hasText()
//            
//        }
//        
//    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
       
        super.init(frame: frame, textContainer: textContainer)
        
        //设置ui
        prepareUI()
        
//        self.delegate = self
        //由于与导航栏右侧按钮的代理冲突，采取通知实现同样的功能
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textViewDidChange:"), name: UITextViewTextDidChangeNotification, object: self)
    }
    
    //取消通知
    deinit{
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //通知的方法
    func textViewDidChange(notification:NSNotification) {
        
        placeHolderLabel.hidden = hasText()
    }
    
    private func prepareUI() {
    
        //添加子控件
        addSubview(placeHolderLabel)
        
        //设置约束
        placeHolderLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)//(?)
        }
        
        
    }
    
    //MARK 懒加载控件
    //站位文本按钮
    private lazy var placeHolderLabel: UILabel = {
    
        let label = UILabel()
        
        //给textview添加站位文本
        label.text = "分享新鲜事..."
        label.textColor = UIColor.lightGrayColor()
        label.sizeToFit()
        
        return label
    }()
    
}

//extension YCLPlaceHolderTextView: UITextViewDelegate {
//
//    func textViewDidChange(textView: UITextView) {
//        
//        placeHolderLabel.hidden = hasText()
//    }
//    
//}

